﻿using UnityEngine;
using System.Collections.Generic;


namespace MyIdolUIFrameWork
{
    public class UIName
    {
        //public const string UITest = "UIScene_Test";
        public const string UIEvaluate = "UIScene_Evaluate";
        public const string UIAwardWord = "UIScene_AwardWords";
        public const string UISeevideo = "UIScene_SeeVideo";
        public const string UIRemoveAD = "UIScene_RemoveAd";
    }
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance;

        public Dictionary<string, UIScene> mUIScene = new Dictionary<string, UIScene>();
        //private Dictionary<UIAnchor.Side, GameObject> mUIAnchor = new Dictionary<UIAnchor.Side, GameObject>();

        void Awake()
        {
            Instance = this;
        }

        public void InitializeUIs()
        {
            //mUIAnchor.Clear();
            //Object[] objs = FindObjectsOfType(typeof(UIAnchor));
            //if (objs != null)
            //{
            //    foreach (Object obj in objs)
            //    {
            //        UIAnchor uiAnchor = obj as UIAnchor;
            //        if (!mUIAnchor.ContainsKey(uiAnchor.side))
            //            mUIAnchor.Add(uiAnchor.side, uiAnchor.gameObject);
            //    }
            //}

            mUIScene.Clear();
            Object[] uis = FindObjectsOfType(typeof(UIScene));
            if (uis != null)
            {
                foreach (Object obj in uis)
                {
                    UIScene ui = obj as UIScene;
                    ui.SetVisible(false);
                    mUIScene.Add(ui.gameObject.name, ui);
                }
            }
        }

        public void SetVisible(string name, bool visible)
        {
            if (visible && !IsVisible(name))
            {
                OpenScene(name);
            }
            else if (!visible && IsVisible(name))
            {
                CloseScene(name);
            }
        }

        public bool IsVisible(string name)
        {
            UIScene ui = GetUI(name);
            if (ui != null)
                return ui.IsVisible();
            return false;
        }
        private UIScene GetUI(string name)
        {
            UIScene ui;
            return mUIScene.TryGetValue(name, out ui) ? ui : null;
        }

        public T GetUI<T>(string name) where T : UIScene
        {
            return GetUI(name) as T;
        }

        private bool isLoaded(string name)
        {
            if (mUIScene.ContainsKey(name))
            {
                return true;
            }
            return false;
        }

        private void OpenScene(string name)
        {
            if (isLoaded(name))
            {
                mUIScene[name].SetVisible(true);
            }
        }
        private void CloseScene(string name)
        {
            if (isLoaded(name))
            {
                mUIScene[name].SetVisible(false);
            }
        }

        /// <summary>	/// 显示一级界面	/// </summary>
        public void SetUIShow()
        {
            //SetVisible(UIName.UITest, true);
            SetVisible(UIName.UIEvaluate, false);
            SetVisible(UIName.UIAwardWord, false);
            SetVisible(UIName.UISeevideo, false);
            SetVisible(UIName.UIRemoveAD, false);
        }
    }
}
